package com.jdz.service.impl;

import com.jdz.common.ServerResponse;
import com.jdz.dao.RaceTypeDetailsMapper;
import com.jdz.dao.RaceTypeMapper;
import com.jdz.dao.RaceTypeSamsaraMapper;
import com.jdz.pojo.*;
import com.jdz.service.IRaceTypeSamsaraService;
import com.jdz.util.DateTimeUtil;
import com.jdz.vo.RaceTypeSamsaraVo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Transactional
@Service("IRaceTypeSamsaraService")
public class RaceTypeSamsaraServiceImpl implements IRaceTypeSamsaraService {

    private Logger logger = LoggerFactory.getLogger(RaceTypeSamsaraServiceImpl.class);


    @Autowired
    RaceTypeSamsaraMapper raceTypeSamsaraMapper;
    @Autowired
    RaceTypeMapper raceTypeMapper;
    @Autowired
    RaceTypeDetailsMapper raceTypeDetailsMapper;

    /**
     * 列出本项目类型的所有比赛等级
     * @param racetypeno
     * @return
     */
    public ServerResponse listRaceTypeSamsara(Integer racetypeno){

        List<RaceTypeSamsara> row = raceTypeSamsaraMapper.selectByRacetypeno(racetypeno);

        List<RaceTypeSamsaraVo> list = new ArrayList<>();
        for(RaceTypeSamsara raceTypeSamsaraItem : row){
            RaceTypeSamsaraVo raceTypeSamsaraVo = new RaceTypeSamsaraVo();
            raceTypeSamsaraVo.setRacetypesamsarano(raceTypeSamsaraItem.getRacetypesamsarano());
            raceTypeSamsaraVo.setRacetypesamsaraname(raceTypeSamsaraItem.getRacetypesamsaraname());
            raceTypeSamsaraVo.setRacetypesamsaragrade(raceTypeSamsaraItem.getRacetypesamsaragrade());
            raceTypeSamsaraVo.setBallotstarttime(DateTimeUtil.dateToStr(raceTypeSamsaraItem.getBallotstarttime(),"yyyy-MM-dd"));
            raceTypeSamsaraVo.setBallotstoptime(DateTimeUtil.dateToStr(raceTypeSamsaraItem.getBallotstoptime(),"yyyy-MM-dd"));
            raceTypeSamsaraVo.setBallotstatus(raceTypeSamsaraItem.getBallotstatus());
            raceTypeSamsaraVo.setRacetypeno(raceTypeSamsaraItem.getRacetypeno());
            raceTypeSamsaraVo.setWeight(raceTypeSamsaraItem.getWeight());
            list.add(raceTypeSamsaraVo);
        }


        return ServerResponse.createBySuccess(list);
    }

    /**
     * 添加比赛等级
     * @param raceTypeSamsaraForm
     * @return
     */
    public ServerResponse add(RaceTypeSamsara raceTypeSamsaraForm){

        List<RaceTypeSamsara> row = raceTypeSamsaraMapper.selectByRacetypeno(raceTypeSamsaraForm.getRacetypeno());

        System.out.println(raceTypeSamsaraForm.getRacetypesamsaraname());
        if(CollectionUtils.isEmpty(row)){
            RaceTypeSamsara raceTypeSamsara = new RaceTypeSamsara();
            raceTypeSamsara.setRacetypeno(raceTypeSamsaraForm.getRacetypeno());
            raceTypeSamsara.setRacetypesamsaraname(raceTypeSamsaraForm.getRacetypesamsaraname());
            raceTypeSamsara.setBallotstarttime(raceTypeSamsaraForm.getBallotstarttime());
            raceTypeSamsara.setBallotstoptime(raceTypeSamsaraForm.getBallotstoptime());
            raceTypeSamsara.setRacetypesamsaragrade(1);

            int r = raceTypeSamsaraMapper.insertSelective(raceTypeSamsara);

            if(r <= 0){

                return ServerResponse.createByErrorMessage("添加比赛等级失败");
            }
            return ServerResponse.createBySuccessMessage("添加比赛等级成功");
        }

        //比较得出等级最高的
        Integer i = row.get(0).getRacetypesamsaragrade();
        for(RaceTypeSamsara raceTypeSamsaraItem : row){
            if(raceTypeSamsaraItem.getRacetypesamsaragrade() > i){
                i = raceTypeSamsaraItem.getRacetypesamsaragrade();
            }
        }

        RaceTypeSamsara raceTypeSamsara = new RaceTypeSamsara();
        raceTypeSamsara.setRacetypeno(raceTypeSamsaraForm.getRacetypeno());
        raceTypeSamsara.setRacetypesamsaraname(raceTypeSamsaraForm.getRacetypesamsaraname());
        raceTypeSamsara.setBallotstarttime(raceTypeSamsaraForm.getBallotstarttime());
        raceTypeSamsara.setBallotstoptime(raceTypeSamsaraForm.getBallotstoptime());
        raceTypeSamsara.setRacetypesamsaragrade(i+1);

        int r = raceTypeSamsaraMapper.insertSelective(raceTypeSamsara);

        if(r <= 0){

            return ServerResponse.createByErrorMessage("添加比赛等级失败");
        }
        return ServerResponse.createBySuccessMessage("添加比赛等级成功");
    }

    /**
     * 修改比赛等级名字，只接收数据库id和修改后的项目名称
     * @param raceTypeSamsaraForm
     * @return
     */
    public  ServerResponse update(RaceTypeSamsara raceTypeSamsaraForm){
        RaceTypeSamsara raceTypeSamsara = new RaceTypeSamsara();
        raceTypeSamsara.setRacetypesamsarano(raceTypeSamsaraForm.getRacetypesamsarano());
        raceTypeSamsara.setRacetypesamsaraname(raceTypeSamsaraForm.getRacetypesamsaraname());
        raceTypeSamsara.setBallotstarttime(raceTypeSamsaraForm.getBallotstarttime());
        raceTypeSamsara.setBallotstoptime(raceTypeSamsaraForm.getBallotstoptime());
        int r = raceTypeSamsaraMapper.updateByPrimaryKeySelective(raceTypeSamsara);
        if(r <= 0){

            return ServerResponse.createByErrorMessage("修改比赛等级名字失败");
        }
        return ServerResponse.createBySuccessMessage("修改比赛等级名字成功");
    }

    /**
     * 删除项目类型比赛等级
     * @param racetypesamsarano
     * @return
     */
    public ServerResponse delete(Integer racetypesamsarano){

        RaceTypeSamsara row = raceTypeSamsaraMapper.selectByPrimaryKey(racetypesamsarano);

        int r = raceTypeSamsaraMapper.deleteByPrimaryKey(racetypesamsarano);
        if(r <= 0){
            return ServerResponse.createByErrorMessage("删除比赛等级名字失败");
        }

        List<RaceTypeSamsara> raceTypeSamsaraList =  raceTypeSamsaraMapper.selectByRacetypeno(row.getRacetypeno());

        int i=1;
        for(RaceTypeSamsara raceTypeSamsaraItem : raceTypeSamsaraList){
            raceTypeSamsaraItem.setRacetypesamsaragrade(i);
            raceTypeSamsaraItem.setCreattime(null);
            int rulth =  raceTypeSamsaraMapper.updateByPrimaryKeySelective(raceTypeSamsaraItem);
            if(rulth <= 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ServerResponse.createByErrorMessage("删除比赛等级名字失败");
            }
            i++;
        }

        return ServerResponse.createBySuccessMessage("删除比赛等级名字成功");

    }


    /**
     * 设置投票开始时间
     * @param racetypesamsarano
     * @param startTime
     * @return
     */
    public ServerResponse setballotStart(Integer racetypesamsarano,String startTime){

        Date startTimeDate;


        try {

            if(StringUtils.isBlank(startTime))
                return ServerResponse.createByErrorMessage("参数错误");
            else
                startTimeDate = DateTimeUtil.dateMax(startTime);

        }catch (Exception e){
            return ServerResponse.createByErrorMessage("输入开始时间参数格式错误");
        }

        RaceTypeSamsara raceTypeSamsara = new RaceTypeSamsara();
        raceTypeSamsara.setRacetypesamsarano(racetypesamsarano);
        raceTypeSamsara.setBallotstarttime(startTimeDate);
        int r = raceTypeSamsaraMapper.updateByPrimaryKeySelective(raceTypeSamsara);
        if(r <= 0){

            return ServerResponse.createByErrorMessage("修改投票开始时间失败");
        }
        return ServerResponse.createBySuccessMessage("修改投票开始时间成功");

    }

    /**
     * 设置投票结束时间
     * @param racetypesamsarano
     * @param stopTime
     * @return
     */
    public ServerResponse setballotStop(Integer racetypesamsarano,String stopTime){

        Date stopTimeDate;


        try {

            if(StringUtils.isBlank(stopTime))
                return ServerResponse.createByErrorMessage("参数错误");
            else
                stopTimeDate = DateTimeUtil.dateMax(stopTime);

        }catch (Exception e){
            return ServerResponse.createByErrorMessage("输入结束时间参数格式错误");
        }

        RaceTypeSamsara raceTypeSamsara = new RaceTypeSamsara();
        raceTypeSamsara.setRacetypesamsarano(racetypesamsarano);
        raceTypeSamsara.setBallotstarttime(stopTimeDate);
        int r = raceTypeSamsaraMapper.updateByPrimaryKeySelective(raceTypeSamsara);
        if(r <= 0){

            return ServerResponse.createByErrorMessage("修改投票结束时间失败");
        }
        return ServerResponse.createBySuccessMessage("修改投票结束时间成功");

    }


    /**
     * 开始投票
     * @param racetypesamsarano
     * @return
     */
    public ServerResponse ballotStart(Integer racetypesamsarano){
        if(racetypesamsarano == null || racetypesamsarano ==0){
            return ServerResponse.createByErrorMessage("参数错误");
        }


//        RaceType raceType = raceTypeMapper.selectByPrimaryKey(racetypeno);
//        if(raceType == null){
//            return ServerResponse.createByErrorMessage("找不到此项目类型");
//        }
//        RaceTypeDetails raceTypeDetails =  raceTypeDetailsMapper.selectByRacetypeno(racetypeno);
//        if(raceTypeDetails == null){
//            return ServerResponse.createByErrorMessage("比赛没开始或者找不到此项目类型详情");
//        }
//
//
//        RaceTypeSamsara raceTypeSamsara =  raceTypeSamsaraMapper.selectByRacetypenoAndRaceTypeSamsaraGrade(raceType.getRacetypeno(),raceTypeDetails.getRacetypedetailsstate());
//
//        if(raceTypeSamsara==null){
//            return ServerResponse.createByErrorMessage("比赛当前轮次在轮次表不存在此轮次，请联系程序员修改bug");
//        }

        RaceTypeSamsara raceTypeSamsara1One = new RaceTypeSamsara();
        raceTypeSamsara1One.setRacetypesamsarano(racetypesamsarano);
        raceTypeSamsara1One.setBallotstatus(0);
        int row2 = raceTypeSamsaraMapper.updateByPrimaryKeySelective(raceTypeSamsara1One);
        if(row2 <= 0){
            return ServerResponse.createByErrorMessage("开始投票失败");
        }
        return ServerResponse.createBySuccessMessage("开始投票成功");


    }

    /**
     * 结束投票
     * @param racetypesamsarano
     * @return
     */
    public ServerResponse ballotStop(Integer racetypesamsarano){
        if(racetypesamsarano == null || racetypesamsarano ==0){
            return ServerResponse.createByErrorMessage("参数错误");
        }


//        RaceType raceType = raceTypeMapper.selectByPrimaryKey(racetypeno);
//        if(raceType == null){
//            return ServerResponse.createByErrorMessage("找不到此项目类型");
//        }
//        RaceTypeDetails raceTypeDetails =  raceTypeDetailsMapper.selectByRacetypeno(racetypeno);
//        if(raceTypeDetails == null){
//            return ServerResponse.createByErrorMessage("比赛没开始或者找不到此项目类型详情");
//        }
//
//
//        RaceTypeSamsara raceTypeSamsara =  raceTypeSamsaraMapper.selectByRacetypenoAndRaceTypeSamsaraGrade(raceType.getRacetypeno(),raceTypeDetails.getRacetypedetailsstate());
//
//        if(raceTypeSamsara==null){
//            return ServerResponse.createByErrorMessage("比赛当前轮次在轮次表不存在此轮次，请联系程序员修改bug");
//        }

        RaceTypeSamsara raceTypeSamsara1One = new RaceTypeSamsara();
        raceTypeSamsara1One.setRacetypesamsarano(racetypesamsarano);
        raceTypeSamsara1One.setBallotstatus(1);
        int row2 = raceTypeSamsaraMapper.updateByPrimaryKeySelective(raceTypeSamsara1One);
        if(row2 <= 0){
            return ServerResponse.createByErrorMessage("截止投票失败");
        }
        return ServerResponse.createBySuccessMessage("截止投票成功");

    }

}
